package com.example.chatchatbackend.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.chatchatbackend.entity.RestBean;
import com.example.chatchatbackend.entity.dto.Products;

import com.example.chatchatbackend.entity.vo.response.ProductsVO;
import com.example.chatchatbackend.service.ProductsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */

@Slf4j
@RestController
@RequestMapping("/api/products")
public class ProductsController {

    @Autowired
    private ProductsService productsService;

    @GetMapping
    public RestBean<List<Products>> getAll(){
        List<Products> productsList = productsService.getAllProducts();
        log.info("查询所有商品:{}",productsList);
        return RestBean.success(productsList);
    }

    @PostMapping
    public RestBean<ProductsVO> insertAdv(@RequestBody ProductsVO productsVO){
        ProductsVO insertProductsVO = productsService.insertProducts(productsVO);
        return RestBean.success(insertProductsVO);

    }

    @PostMapping("/list")
    public RestBean<List<ProductsVO>> insertAdvList (@RequestBody List<ProductsVO> productsVO){
        List<ProductsVO> ProductsList = productsService.insertProductsList(productsVO);
        return RestBean.success(ProductsList);
    }
//
//    @GetMapping("/{Id}")
//    public RestBean<List<Products>> getProductsById(@PathVariable Integer Id){
//        List<Products> productsList = productsService.getProductsById(Id);
//        log.info("查某一询商品详细信息:{}",productsList);
//        return RestBean.success(productsList);
//    }
//
//    @GetMapping("/c/{categoryId}")
//    public RestBean<List<Products>> getProductsByCategoryId(@PathVariable Integer categoryId){
//        List<Products> productsList = productsService.getProductsByCategoryId(categoryId);
//        log.info("查询某一种类的所有商品:{}",productsList);
//        return RestBean.success(productsList);
//    }

    @GetMapping("/pageThree")
    public RestBean<IPage<Products>> pageThree(@RequestParam(value = "pageNum" ,defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        return RestBean.success(productsService.pageThree(pageNum, pageSize));
    }

    @PutMapping
    public RestBean<ProductsVO> updateProduct(@RequestBody ProductsVO productsVO){
        return RestBean.success(productsService.updateProducts(productsVO));

    }

    @DeleteMapping("/ids")
    public RestBean<String> delete(@RequestBody List<Integer> ids) {
        productsService.deleteByIds(ids);
        return RestBean.success("删除成功");
    }

    @GetMapping("/ids")
    public RestBean<List<ProductsVO>> findByIds(@RequestBody List<Integer> ids){
        List<ProductsVO> productsVOList = productsService.findByIds(ids);
        return RestBean.success(productsVOList);
    }
}
